package com.tom.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @version V1.0
 * @Package bw-article-api
 * @Description:
 * @author: dengyi
 * @date: 2020/2/25
 * @Copyright: 2019 www.tomonline-inc.com Inc. All rights reserved.
 * 注意：本内容仅限于TOM集团内部传阅，禁止外泄以及用于其他的商业目
 */
@org.springframework.stereotype.Service
@Slf4j
public class Service {


    @Value("${bw-article.IMG_SERVER_URL}")
    private String imgServerurl;

    //@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
    public String saveImage(String url, String channelname) {
        if (StringUtil.isBlank(url)) {
            return "";
        }
        log.info("图片的下载：{}", url);
        HttpRequest request = HttpRequest.get(imgServerurl);
        request.timeout(20 * 1000);
        request.query("URL", url);
        request.query("path", "import/" + channelname + "/images");
        log.info("访问图片下载:{}", request);
        HttpResponse response = null;
        try {
            response = request.send();
            String respBody = response.bodyText();
            log.info("保存图片返回结果:{}", respBody);
            JSONObject jsonObject = JSON.parseObject(respBody);
            String thumbnail = jsonObject.getString("fileurl");
            return thumbnail;
        } catch (Exception e) {
            log.error("保存图片失败", e);
            return "";
        }
    }


}
